package com.seda.fdc.ejb.reports.pdfgenerator.lowagie.text.image;

import java.awt.Color;
import java.io.IOException;
import java.net.MalformedURLException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.*; 

public abstract class AbstractImageBox{
	static int PAINTING = 0;
	static int PAINTED = 1;
	

	public static final int ADAPT_POLICY_NONE = 1;
	public static final int ADAPT_POLICY_UNDER = 2;
	public static final int ADAPT_POLICY_OVER = 3;
	public static final int ADAPT_POLICY_ALWAYS = 4;
	
	
	
	
	
	static final float defaultDpiImage = 72;
	
	private int adaptPolicy;
	private PdfPTable table;
	private PdfPCell cell; 
	private Image image;
	private int stato;
	
	
	public AbstractImageBox(){
		init();
	}
	

	public AbstractImageBox(float x, float y){  
		init();

		table.setTotalWidth(x);
		table.setLockedWidth(true);
		cell.setFixedHeight(y);  
	}
	
	
	private void init(){ 
		stato = PAINTING;
		adaptPolicy = ADAPT_POLICY_UNDER;
		
		table = new PdfPTable(1);
		table.getDefaultCell().setBorderWidth(0f); 
		cell = new PdfPCell();
		cell.setPadding(0f);
		cell.setBorderWidth(0f);
	}
	
 
	
	
	public void setDimension(float x, float y) throws IllegalStateException{
		checkStato();
		table.setTotalWidth(x);
		table.setLockedWidth(true);
		cell.setFixedHeight(y);  
	}
	

	protected void setAbsolutePosition(float x, float y, PdfContentByte pdfContentByte) throws IllegalStateException, BadElementException, MalformedURLException, IOException{
		checkStato(); 
		 
		doAdapt();
		
		cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
		
		if(image != null){

			cell.setPaddingLeft( (float) (( table.getTotalWidth() -   image.getScaledWidth()  ) / 2)  ) ;
			//System.out.println(  (float) (( table.getTotalWidth() - image.getWidth() * image.getScaledWidth() /100  ) / 2)  ); 
			cell.addElement(image);
		}
		table.addCell(cell); 
		
		table.writeSelectedRows(0, -1, x, y, pdfContentByte); 
		
		stato = PAINTED;
		 
	}
 

	
	public void setAbsolutePosition(float x, float y, PdfWriter writer) throws IllegalStateException, BadElementException, MalformedURLException, IOException{ 
		this.setAbsolutePosition(x, y, writer.getDirectContent() );
	}
  
	public void addImage(String path ) throws BadElementException, MalformedURLException, IOException, IllegalStateException{
		checkStato();
		image =  Image.getInstance(path); 
		image.scalePercent(100);
		}
	 

	public void addImage(byte[] bytes ) throws BadElementException, MalformedURLException, IOException, IllegalStateException{
		checkStato();
		try { 
			image =  Image.getInstance( bytes );
			image.scalePercent(100);
		} catch (IOException e) { 
		}
		
	}
	

	public void addImage(byte[] bytes , int imageRotation) throws BadElementException, MalformedURLException, IOException, IllegalStateException{
		checkStato();
		try { 
			image =  Image.getInstance( bytes );
			image.setRotation(imageRotation);
			image.scalePercent(100);
		} catch (IOException e) { 
		}
		
	}
	
	private void adaptUnder() throws BadElementException, MalformedURLException, IOException , IllegalStateException{
		checkStato();
		  
		if(image != null){
			double boxHeight, boxWidth, imageHeight, imageWidth;
			double scalePercent;
			
			boxWidth = table.getTotalWidth();
			boxHeight = cell.getFixedHeight();
			imageHeight= image.getHeight();
			imageWidth = image.getWidth();
			
			if( imageHeight > boxHeight || imageWidth > boxWidth ){
				scalePercent = 100 * Math.min( 
							(boxWidth / imageWidth), 
							(boxHeight / imageHeight)
							)	;
				image.scalePercent((float) scalePercent); 
			}
		}
	}
	

	public void setAdaptPolicy(int policy) throws IllegalStateException{ 
		checkStato();
		adaptPolicy = policy;
	}
	
	
	private void doAdapt() throws BadElementException, MalformedURLException, IOException , IllegalStateException{
		switch (adaptPolicy) {
			case ADAPT_POLICY_ALWAYS:
				adaptAlways();
				break;
			case ADAPT_POLICY_UNDER:
				adaptUnder();
				break;
			case ADAPT_POLICY_OVER:
				adaptOver();
				break;
			 
 
		}
	}
		
	
	private void adaptAlways() throws BadElementException, MalformedURLException, IOException , IllegalStateException{
		checkStato();
		  
		if(image != null){
			double boxHeight, boxWidth, imageHeight, imageWidth; 
			
			boxWidth = table.getTotalWidth();
			boxHeight = cell.getFixedHeight();
			imageHeight= image.getHeight();
			imageWidth = image.getWidth();

			if( imageHeight > boxHeight || imageWidth > boxWidth ){
				adaptUnder();
			} else{
				adaptOver();
			}
		}
	}

	private void adaptOver() throws BadElementException, MalformedURLException, IOException , IllegalStateException{
		checkStato();
		  
		if(image != null){
			double boxHeight, boxWidth, imageHeight, imageWidth;
			double scalePercent;
			
			boxWidth = table.getTotalWidth();
			boxHeight = cell.getFixedHeight();
			imageHeight= image.getHeight();
			imageWidth = image.getWidth();
			
			if( imageHeight < boxHeight && imageWidth < boxWidth ){
				scalePercent = 99 * Math.max( 
							(boxWidth / imageWidth), 
							(boxHeight / imageHeight)
							)	;
				image.scalePercent((float) scalePercent); 
			}
		}
	}
	

	public void scalePercentImage(double scalePercent ) throws BadElementException, MalformedURLException, IOException , IllegalStateException{
		checkStato();
		if(image != null){
			image.scalePercent(  image.getHeight() );  
		}
	}
 
	public void setDpiImage(double dpi ) throws BadElementException, MalformedURLException, IOException , IllegalStateException{
		checkStato(); 
		if(image != null){
	//		image.scalePercent((float) ( 100* defaultDpiImage / dpi  ) );  
			image.setDpi((int)dpi, (int)dpi);
		}
	}
	
	public void setRotation(int angle) throws IllegalStateException{
		checkStato();
		cell.setRotation(angle);
	} 
	
	public void setBorderWidth(float width) throws IllegalStateException{
		checkStato();
		cell.setBorderWidth(width);
	}
	
	public void setBorderColor(Color arg0) throws IllegalStateException , IllegalStateException{
		checkStato();
		checkStato();
		cell.setBorderColor(arg0);
	}
	
	
	private void checkStato() throws IllegalStateException{
		if (stato == PAINTED) {
			throw new IllegalStateException("Impossibile chiamare il metodo suddetto dopo aver posizionato l'oggetto");
		} 
	}
}
